#include "Hw_ENC28J60.h"
#include "ENC28J60_Config.h"
#include "ENC28J60.h"
#include "spi.h"
//*****************************************************************************
//
//! \brief Read the value of ENC28J60 ETH register
//!
//! \param Reg   : ENC28J60 Register number
//!
//! \ref None 
//!
//! \note FIGURE 4-3 READ CONTROL REGISTER COMMAND SEQUENCE (ETH REGISTERS)
//!        ___                                                                  ____  
//!  /C/S:    \________________________________________________________________/ 
//!             _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _
//!   SCK:_____|0|_|1|_|2|_|3|_|4|_|5|_|6|_|7|_|8|_|9|_|A|_|B|_|C|_|D|_|E|_|F|_______  
//!               Opcode          Address
//!           |-----------|-------------------|
//!       ___  __  __  __  __  __  __  __  __  ______________________________________
//!   SI :   \/0 \/0 \/0 \/4 \/3 \/2 \/1 \/0 \/                                                                   
//!       ___/\__/\__/\__/\__/\__/\__/\__/\__/\______________________________________
//!                
//!                High-impedance State        __  __  __  __  __  __  __  __
//!   SO :____________________________________/7 \/6 \/5 \/4 \/3 \/2 \/1 \/0 \_______                                                                
//!                                           \__/\__/\__/\__/\__/\__/\__/\__/
//!
//! \note FIGURE 4-4 READ CONTROL REGISTER COMMAND SEQUENCE (MAC AND MII REGISTERS)
//!        ___                                                                                                  ___  
//!  /C/S:    \________________________________________________________________________________________________/ 
//!             _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _
//!   SCK:_____|0|_|1|_|2|_|3|_|4|_|5|_|6|_|7|_|8|_|9|_|A|_|B|_|C|_|D|_|E|_|F|_|0|_|1|_|2|_|3|_|4|_|5|_|6|_|7|_____  
//!               Opcode          Address
//!           |-----------|-------------------|
//!       ___  __  __  __  __  __  __  __  __  ____________________________________________________________________
//!   SI :   \/0 \/0 \/0 \/4 \/3 \/2 \/1 \/0 \/                                                                   
//!       ___/\__/\__/\__/\__/\__/\__/\__/\__/\____________________________________________________________________
//!                
//!                High-impedance State        __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __
//!   SO :____________________________________/7 \/6 \/5 \/4 \/3 \/2 \/1 \/0 \/7 \/6 \/5 \/4 \/3 \/2 \/1 \/0 \_____                                                                  
//!                                           \__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/
//!
//! \return The value of ENC28J60 register.
//
//*****************************************************************************
u8 ENC28J60_Read_Control_Register(u8 OPCODE,u8 Reg)
{
	u8 ETH_Reg_Temp = 0;
	ETH_Reg_Temp = (ENC28J60_RCR_OPCODE | (Reg & ADDR_MASK)); 
	SPIx_ReadWriteByte(ETH_Reg_Temp);
	ETH_Reg_Temp = SPIx_ReadWriteByte(0);
	return ETH_Reg_Temp;
}
//*****************************************************************************
//
//! \brief Read the value of ENC28J60 MAC and MII register
//!
//! \param Reg   : ENC28J60 Register number
//!
//! \ref None 
//!

//!
//! \return The value of ENC28J60 register.
//
//*****************************************************************************
u16 ENC28J60_Read_Control_MAC_MII_Register(u8 Reg)
{
	u8 MAC_MII_Reg_Temp = 0;
	u8 Read_Temp_Data1 = 0;
	u8 Read_Temp_Data2 = 0;
	MAC_MII_Reg_Temp = (ENC28J60_RCR_OPCODE | Reg); 
	SPIx_ReadWriteByte(MAC_MII_Reg_Temp);
	Read_Temp_Data1 = SPIx_ReadWriteByte(0);
	Read_Temp_Data2 = SPIx_ReadWriteByte(0);
	return ((Read_Temp_Data1 << 8) + Read_Temp_Data2);
	
}
//*****************************************************************************
//
//! \brief Write data in to Control register
//!
//! \param Reg   : ENC28J60 Register number
//!        Data  : The value will write to the register
//!
//! \ref None 
//!
//! \note FIGURE 4-5 WRITE CONTROL REGISTER COMMAND SEQUENCE (ETH REGISTERS)
//!        ___                                                               ____  
//!  /C/S:    \_____________________________________________________________/ 
//!             _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _
//!   SCK:_____|0|_|1|_|2|_|3|_|4|_|5|_|6|_|7|_|8|_|9|_|A|_|B|_|C|_|D|_|E|_|F|______  
//!               Opcode          Address               Data Byte
//!           |-----------|-------------------|-------------------------------|
//!       ___  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  
//!   SI :   \/0 \/0 \/0 \/4 \/3 \/2 \/1 \/0 \/7 \/6 \/5 \/4 \/3 \/2 \/1 \/0 \__                                               
//!       ___/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/
//!                
//!                             High-impedance State         
//!   SO :______________________________________________________________________ 
//!
//! \return None.
//
//*****************************************************************************
void ENC28J60_Write_Control_Register(u8 Reg,u8 Data)
{
	u8 Reg_Temp = 0;
	Reg_Temp = (ENC28J60_WCR_OPCODE | Reg); 
	SPIx_ReadWriteByte(Reg_Temp);
	SPIx_ReadWriteByte(Data);
	
}
//*****************************************************************************
//
//! \brief Write 2 bite data in to Buffer
//!
//! \param Data1  : The 1st byte data will write to the register
//!        Data2  : The 2st byte data will write to the register
//!
//! \ref None 
//!
//!                                             
//! \note FIGURE 4-6 READ CONTROL REGISTER COMMAND SEQUENCE (MAC AND MII REGISTERS)
//!        ___                                                                                                  ___  
//!  /C/S:    \________________________________________________________________________________________________/ 
//!             _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _   _
//!   SCK:_____|0|_|1|_|2|_|3|_|4|_|5|_|6|_|7|_|8|_|9|_|A|_|B|_|C|_|D|_|E|_|F|_|0|_|1|_|2|_|3|_|4|_|5|_|6|_|7|_____  
//!               Opcode          Address                Data Byte0                     Data Byte1
//!           |-----------|-------------------|-------------------------------|-------------------------------|
//!       ___  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __  __
//!   SI :   \/0 \/0 \/0 \/4 \/3 \/2 \/1 \/0 \/7 \/6 \/5 \/4 \/3 \/2 \/1 \/0 \/7 \/6 \/5 \/4 \/3 \/2 \/1 \/0 \_____      
//!       ___/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/\__/
//!                
//!                                              High-impedance State        
//!   SO :________________________________________________________________________________________________________ 
//!
//! \return None.
//
//*****************************************************************************
void ENC28J60_Write_Buffer(u8 Data1,u8 Data2)
{
	SPIx_ReadWriteByte(ENC28J60_WBM_OPCODE);
	SPIx_ReadWriteByte(Data1);
	SPIx_ReadWriteByte(Data2);
	
}
//*****************************************************************************
//
//! \brief Reset ENC28J60
//!
//! \param None  
//!
//! \ref None 
//!
//! \note None
//!
//! \return None.
//
//*****************************************************************************
void ENC28J60_System_Reset(void)
{
	SPIx_ReadWriteByte(ENC28J60_SRC_OPCODE);
}

//*****************************************************************************
//
//! \brief Init ENC28J60
//!
//! \param None  
//!
//! \ref None 
//!
//! \note None
//!
//! \return None.
//
//*****************************************************************************
void ENC28J60_Init(void)
{
	
	
}






